package com.bosssoft.springcloud.controller;

import com.bosssoft.springcloud.entity.dto.PermissionDTO;
import com.bosssoft.springcloud.entity.dto.RoleDTO;
import com.bosssoft.springcloud.entity.dto.UserDTO;
import com.bosssoft.springcloud.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class RbacController {
    @Autowired
    UserService userService;
    @Resource
    RoleService roleService;
    @Autowired
    PermissionService permissionService;

    /***
     * 根据用户名获取其权限，然后跳转至指定页面
     * @param username 用户名
     * @return
     */
    @GetMapping("/rbac/permission")
    public String showPage(String username) {
        ArrayList<PermissionDTO> permissions = new ArrayList<>();
        //通过用户名获取用户
        UserDTO user = userService.findUserByUsername(username);
        //通过用户信息获取其角色信息
        RoleDTO role = roleService.findRoleById(user.getRid());
        //通过其角色信息获取其权限
        List<Integer> permissionsId = role.getPermissionsId();
        for (int permission: permissionsId) {
            //将该角色权限存放进permissions
            permissions.add(permissionService.findPermissionById(permission));
        }
        for (PermissionDTO permissionDTO: permissions) {
            if ("管理员权限".equals(permissionDTO.getPermissionName())){

                return "管理员界面";
            }
        }
        return "普通用户界面";
    }


}
